home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / source.exe / POSIX / FIND / MAIN.C < prev    next >
C/C++ Source or Header  |  1993-06-23  |  5KB  |  188 lines

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)main.c    5.9 (Berkeley) 5/24/91";
  36. #endif /* not lint */
  37.  
  38. #ifdef DF_POSIX    /* DF_MSS */
  39. #include <misc.h>
  40. #include <bsdlib.h>
  41. #endif
  42.  
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <fcntl.h>
  46. #include <time.h>
  47. #include <fts.h>
  48. #include <errno.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <limits.h>
  52. #include <string.h>
  53. #if _POSIX_SOURCE
  54. # include <unistd.h>
  55. #endif
  56. #include "find.h"
  57.  
  58. time_t now;            /* time find was run */
  59. #ifdef _POSIX_SOURCE /* DF_DSC */
  60.     char curr_dir[PATH_MAX +1];   /* starting directory */
  61.     char *curr_dir_ptr;
  62. #else
  63.     int dotfd;            /* starting directory */
  64. #endif
  65. int ftsoptions;            /* options for the ftsopen(3) call */
  66. int isdeprecated;        /* using deprecated syntax */
  67. int isdepth;            /* do directories on post-order visit */
  68. int isoutput;            /* user specified output operator */
  69. int isxargs;            /* don't permit xargs delimiting chars */
  70.  
  71. #if WIN_NT
  72. extern int globulate __P((int, int, char **));
  73. extern void deglobulate __P((void));
  74. extern int globulated_argc;
  75. extern char **globulated_argv;
  76. pid_t ppid;
  77. int globulation;
  78. #endif
  79.  
  80. static void usage __P((void));
  81.  
  82. int
  83. #if __STDC__
  84. main (int argc, char **argv)
  85. #else
  86. main(argc, argv)
  87.     int argc;
  88.     char **argv;
  89. #endif
  90. {
  91.     register char **p, **start;
  92.     PLAN *find_formplan __P((char **));
  93.     int ch;
  94.  
  95. #if WIN_NT
  96.     ppid = getppid();
  97.     if (ppid == (pid_t) 1) /* if parent is CMD.EXE */
  98.     {
  99.         globulation = globulate(1, argc, argv);
  100.         if (globulation == 0)
  101.         {
  102.             argc = globulated_argc;
  103.             argv = globulated_argv;
  104.         }
  105.     }
  106. # if 0
  107.     (void) fprintf(stderr, "%s", *argv);
  108.     for (ch = 1; ch < argc; ++ch)
  109.     {
  110.         (void) fprintf(stderr, " %s", argv[ch]);
  111.     }
  112.     (void) fputc('\n', stderr);
  113.     (void) fflush(stderr);
  114. # endif
  115. #endif
  116.     (void)time(&now);    /* initialize the time-of-day */
  117.  
  118.     p = start = argv;
  119.     ftsoptions = FTS_NOSTAT|FTS_PHYSICAL;
  120.     while ((ch = getopt(argc, argv, "df:sXx")) != EOF) {
  121. #if 0 && DF_POSIX
  122.     printf("opt: %c", ch);
  123. #endif
  124.         switch(ch) {
  125.         case 'd':
  126.             isdepth = 1;
  127.             break;
  128.         case 'f':
  129.             *p++ = optarg;
  130.             break;
  131.         case 's':
  132.             ftsoptions &= ~FTS_PHYSICAL;
  133.             ftsoptions |= FTS_LOGICAL;
  134.             break;
  135.         case 'X':
  136.             isxargs = 1;
  137.             break;
  138.         case 'x':
  139.             ftsoptions &= ~FTS_NOSTAT;
  140.             ftsoptions |= FTS_XDEV;
  141.             break;
  142.         case '?':
  143.         default:
  144.             break;
  145.         }
  146.     }
  147.     argc -= optind;    
  148.     argv += optind;
  149.     /* Find first option to delimit the file list. */
  150.     while (*argv) {
  151.         if (option(*argv))
  152.             break;
  153.         *p++ = *argv++;
  154.     }
  155.     if (p == start)
  156.         usage();
  157.     *p = NULL;
  158.  
  159. #ifdef _POSIX_SOURCE /* DF_DSC: because fchdir (in function.c) not in POSIX */
  160.         if ((curr_dir_ptr = getcwd(curr_dir, sizeof(curr_dir))) == NULL)
  161. #else
  162.     if ((dotfd = open(".", O_RDONLY, 0)) < 0)
  163. #endif
  164.         err(".: %s", strerror(errno));
  165.     find_execute(find_formplan(argv), start);
  166. #if WIN_NT
  167.     if (ppid == (pid_t) 1 && globulation == 0)
  168.         deglobulate();
  169. #endif
  170.     return EXIT_SUCCESS;
  171. }
  172.  
  173. static void
  174. #if __STDC__
  175. usage (void)
  176. #else
  177. usage()
  178. #endif
  179. {
  180.     (void)fprintf(stderr,
  181.         "usage: find [-dsXx] [-f file] [file ...] expression\n");
  182. #if WIN_NT
  183.     if (ppid == (pid_t) 1 && globulation == 0)
  184.         deglobulate();
  185. #endif
  186.     exit(EXIT_FAILURE);
  187. }
  188.